package com.eslserver.test;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eslserver.dao.CoordinatorDAO;
import com.eslserver.dao.DAOException;
import com.eslserver.dao.ESLDAO;
import com.eslserver.dao.ESLServerDAOFactory;
import com.eslserver.to.Coordinator;
import com.eslserver.to.ESL;

@SuppressWarnings("serial")
public class HelloWorld extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			ESLDAO eslDAO = ESLServerDAOFactory.getDAOFactory(
					ESLServerDAOFactory.ESLSERVER_DAOFACTORY).getESLDAO();
			ESL esl = eslDAO.loadESL(1);

			// test mappedBy annotation
			CoordinatorDAO coorDAO = ESLServerDAOFactory.getDAOFactory(
					ESLServerDAOFactory.ESLSERVER_DAOFACTORY)
					.getCoordinatorDAO();
			Coordinator coor = coorDAO.loadCoordinatorByID(1);
			ESL testEsl = ESL.createESL();

			// adding or changing a new ESL from Coordinator should not have any
			// effect due to mappedBy annotation
			coor.getEsls().iterator().next().setName("changedName");
			testEsl.setName("test");
			coor.getEsls().add(testEsl);
			coorDAO.updateCoordinator(coor);

			// test association via cascade, changing coordinator should NOT
			// take effect as there is no cascade rules set
			esl.getCoordinator().setName("changedName");
			esl.getCoordinator().setIpAddress("1.1.1.1");
			eslDAO.updateESL(esl);
			esl.toString();
		} catch (DAOException e) {
			e.printStackTrace();
		}
	}
}
